<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      class="gl_dialog_TitleLine gl_dl_footer"
      :close-on-click-modal="false"
      width="632px"
    >
      <div v-loading="loading" class="gl_px_20">
        <el-form ref="selectedMsg" class="gl_form_mini" :model="selectedMsg" :rules="selectedMsg_rules" label-position="top" label-width="120px" style="padding:0 30px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="选择加减" prop="isOnline">
                <el-radio-group v-model="selectedMsg.isOnline">
                  <el-radio :label="true">加</el-radio>
                  <el-radio :label="false">减</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="增减数值" prop="points">
                <el-input v-model.number="selectedMsg.points" placeholder="请输入" :maxlength="8" type="number" size="mini" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="cancelDialog">取 消</el-button>
        <el-button size="medium" type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import validator from '@/lib/validate/index.js';
import { api_POST_EventsManagementEventResultSetPlatformOperatePoints } from '@/extend/api/gameswaggerApi/app';
import { SiSTKWEventsManagementSetPlatformOperatePointsInput } from '@/lib/form/game/swaggerForm';
export default {
  data() {
    return {
      title: '积分修改',
      dialogVisible: false,
      loading: false,
      selectedMsg: new SiSTKWEventsManagementSetPlatformOperatePointsInput(),
      selectedMsg_rules: {
        isOnline: [{ required: true, message: '请选择加减', trigger: 'blur' }],
        points: [
          { required: true, message: '请输入数值', trigger: 'blur' },
          {
            validator(rule, value, callback) {
              callback(validator.checkPrice(value, '请输入大于0且至多保留两位小数的数值'));
            },
            trigger: 'blur'
          }]
      }
    };
  },
  created() {
    this.$set(this.selectedMsg, 'isOnline', null);
  },
  methods: {
    confirm() {
      const that = this;
      this.$refs.selectedMsg.validate(valid => {
        if (valid) {
          if (this.selectedMsg.points > this.selectedMsg.realPoints && !this.selectedMsg.isOnline) {
            that.$message({ type: 'warning', message: '实际展示积分不能为负数' });
            return;
          }
          this.selectedMsg.points = this.selectedMsg.isOnline ? this.selectedMsg.points : this.selectedMsg.points * -1;
          that.loading = true;
          api_POST_EventsManagementEventResultSetPlatformOperatePoints(that.selectedMsg).then(res => {
            that.loading = false;
            that.dialogVisible = false;
            that.$message({ type: 'success', message: '更新成功' });
            that.selectedMsg = new SiSTKWEventsManagementSetPlatformOperatePointsInput();
            that.$parent.getPageList();
          }, error => {
            that.loading = false;
            const _msg = error.error.message || error.message;
            that.$message({ type: 'error', message: _msg });
          });
        } else {
          that.dialogVisible = true;
          return false;
        }
      });
    },
    cancelDialog() {
      this.dialogVisible = false;
      this.selectedMsg = new SiSTKWEventsManagementSetPlatformOperatePointsInput();
    }
  }
};
</script>

<style lang="css" scoped>
</style>
